$(function() {
    /* 获取博客详情*/
    var recordcount;
    var pageid = 1;

    function content() {
        var url = 'https://yuyoublog.cn/asp/content.asp?action=read_content&pageId=' + pageid;
        $.ajax(url, {
            dataType: 'json',
            async: true,
            type: 'get',
            headers: {
                'Content-Type': undefined
            },
            timeout: 5000,
            success: function(data) {
                recordcount = data.recordcount; // 获取总记录数
                localStorage.setItem("recordcounts", recordcount); // 更新为实际的recordcount
                var content = data.AdminList;
                var contentHtml = '';
                $.each(content, function(i, value) {
                    contentHtml += `
                    <div class="center">
                     <p style="display:none;" class="valueid">${value.id}</p>
                        <img src="${value.imgaes}" alt="${value.title}"/>
                        <h4>${value.title}</h4>
                        <p>${value.brief}</p>
                        <div class="xiaobiao">
                        日期：<span>${value.create_date}</span>&nbsp; 标签：<a>${value.label}</a>
                        <a class="jixuyuedu">继续阅读</a>
                        </div>
                    </div>
                    `;
                });
                $(".zuiclasss").html(contentHtml);
                $("img").lazyload();

                // 初始化分页
                initPagination();
            },
            error: function(xhr, type, errorThrown) {
                console.log(errorThrown);
            }
        });
    }

    function initPagination() {
        $('.bootstrap-iso #pagination1').jqPaginator({
            totalCounts: recordcount, // 设置分页的总条目数
            pageSize: 6,
            visiblePages: 2, // 设置最多显示的页码数
            currentPage: Number(localStorage.getItem("page")) || 1, // 设置当前的页码
            first: '<li class="first"><a>首页</a></li>',
            prev: '<li class="prev"><a>上一页</a></li>',
            next: '<li class="next"><a>下一页</a></li>',
            last: '<li class="last"><a>末页</a></li>',
            page: '<li class="page"><a>{{page}}</a></li>',
            onPageChange: function(num, type) {
                if (type === "change") {
                    pageid = num;
                    content(); // 重新加载内容
                    localStorage.setItem("page", pageid);
                    $("body,html").animate({ scrollTop: 0 }, 500);
                }
            }
        });
    }

    content(); // 初始化内容加载
});
